package com.aisile.utils.test;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Auther:yry
 * @Date:2024/7/12 0012
 * @VERSON:1.0
 */
public class JWTTestDemo {

    public static void main(String[] args) {

        Map<String,Object> claimMap = new HashMap<>();
        claimMap.put("name","admin");
        claimMap.put("roles","admin,user");

        // 登录成功使用
        String str =  Jwts.builder().setId(UUID.randomUUID().toString())
                .setClaims(claimMap).setSubject("签发令牌").setIssuer("签发人")
                .signWith(SignatureAlgorithm.HS256,"aisile").setExpiration(new Date()).compact();
        System.out.println(str);

        // 如果有人篡改jwt token串  name会解析报错
        // 每一个接口 都用  网关
        Claims claims =  Jwts.parser().setSigningKey("aisile").parseClaimsJws(str).getBody();
        System.out.println(claims);
        System.out.println(claims.get("name"));

        //eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiLnrb7lj5Hku6TniYwiLCJyb2xlcyI6ImFkbWluLHVzZXIiLCJuYW1lIjoiYWRtaW4iLCJpc3MiOiLnrb7lj5HkuroifQ.-dIfbNjwXfTFzeC8xtxScQg4PNNfeufmmIjwaPR0R9A
    }

}
